package com.pounds.draggedview;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;

import com.pounds.view.ButtonResponseBean;
import com.pounds.view.DraggableGridViewPager;
import com.pounds.view.ICBCButtonAdapter;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private List<ButtonResponseBean> icbcList;
    private ICBCButtonAdapter mAdapter;
    public boolean isShowing=false;
    private DraggableGridViewPager mDraggableGridViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mDraggableGridViewPager = (DraggableGridViewPager) findViewById(R.id.draggable_grid_view_pager);
        icbcList=new ArrayList<>();
        initView();
    }

    private void initView() {
        // 工行工具栏初始化
        for(int i=0;i<7;i++) {
            ButtonResponseBean ib=new ButtonResponseBean();
            ib.setButtonPic("http://www.androidchina.net/wp-content/uploads/2015/07/2345644_android-1-220x150.jpg");
            ib.setButtonTitle("button "+i);
            icbcList.add(ib);
        }

        mAdapter=new ICBCButtonAdapter(this, icbcList, R.layout.draggable_grid_item);
        mDraggableGridViewPager.setAdapter(mAdapter);
        mDraggableGridViewPager.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position == 4) {
                    if (isShowing) { //收起
                        mAdapter.hideItems();
                        isShowing = false;

                    } else { //展开
                        mAdapter.showItems();
                        isShowing = true;

                    }
                }
                Toast.makeText(getApplicationContext(),"你按了 button"+position,Toast.LENGTH_SHORT).show();
            }
        });
        mDraggableGridViewPager.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(getApplicationContext(),"你长按了 button"+position,Toast.LENGTH_SHORT).show();
                return true;
            }
        });
        mDraggableGridViewPager.setOnRearrangeListener(new DraggableGridViewPager.OnRearrangeListener() {
            @Override
            public void onRearrange(int oldIndex, int newIndex) {

                mAdapter.swapItem(oldIndex, newIndex);
                mAdapter.notifyDataSetChanged();

            }
        });
        mAdapter.hideItems();
    }
}
